/*
    Project Milestone 3
    by Lorenzo Miori [Lorenzo.Miori@stud-inf.unibz.it,
    Matteo Angriman [Matteo.Angriman@stud-inf.unibz.it],
    Nick Preda [Nicolae.Preda@stud-inf.unibz.it]
*/

package GUI;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.HashMap;
import javax.swing.JFrame;

/**
 *
 * @author lorenzo
 */
public abstract class BasicManageGUI extends JFrame {

    private HashMap<String, String> currentEntry;
    private DataModifiedListener changeListener;

    /**
     * Creates a new basic form to be extended by implementer
     */
    public BasicManageGUI(DataModifiedListener changeListener, HashMap<String, String> currentEntry) {
        super();
        this.changeListener = changeListener;
        this.currentEntry = currentEntry;
        initComponents();
        finalSetup();
    }
    
    protected void finalSetup() {
        setDefaultCloseOperation(HIDE_ON_CLOSE);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation((screenSize.width - getWidth()) / 2,
                                 (screenSize.height - getHeight()) / 2);
        setVisible(true);
    }
        
    /**
     * Utility function to return an empty string instead of a null, in case
     * input string is null.
     * @param input a String
     * @return a String, empty if the parameter is null, the same as input otherwise
     */
    protected String nullSentinel(String input) {
        if (input == null) {
            return "";
        }
        else {
            return input;
        }
    }
    
    protected String nullSentinelInteger(String input) {
        if (input == null) {
            return "0";
        }
        else {
            return input;
        }
    }
    
    protected char nullSentinelBoolean(String input) {
        if (input == null || input.length() == 0) {
            return 'f';
        }
        else {
            return input.charAt(0);
        }
    }
    
    protected void setTitle(String title, String description) {
        setTitle(title);
        formWindowTitle.setText(description);
    }
    
    public void destroyWindow() {
        setVisible(false);
        dispose();
    }
    
    /**
     * Returns current data, no matter if it has been updated or not:
     * it's always the most updated data (i.e before saving into database)
     * @return HashMap of String, String containing latest data
     */
    protected HashMap<String, String> getCurrentData() {
        return currentEntry;
    };
    
    /**
     * Refresh current data, upon modification for example
     * @param data 
     */
    protected abstract void refreshCurrentData();

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        formWindowTitle = new java.awt.Label();
        jPanel1 = new javax.swing.JPanel();
        saveButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        mainAreaPanel = new javax.swing.JPanel();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        formWindowTitle.setAlignment(java.awt.Label.CENTER);
        formWindowTitle.setFont(new java.awt.Font("Dialog", 1, 15)); // NOI18N
        formWindowTitle.setText("<Place your title here>");
        getContentPane().add(formWindowTitle, java.awt.BorderLayout.PAGE_START);

        jPanel1.setPreferredSize(new java.awt.Dimension(600, 50));

        saveButton.setText("Save");
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cancelButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 284, Short.MAX_VALUE)
                .addComponent(saveButton)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(saveButton))
                .addContainerGap(13, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_END);

        mainAreaPanel.setPreferredSize(new java.awt.Dimension(500, 300));

        javax.swing.GroupLayout mainAreaPanelLayout = new javax.swing.GroupLayout(mainAreaPanel);
        mainAreaPanel.setLayout(mainAreaPanelLayout);
        mainAreaPanelLayout.setHorizontalGroup(
            mainAreaPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 457, Short.MAX_VALUE)
        );
        mainAreaPanelLayout.setVerticalGroup(
            mainAreaPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 263, Short.MAX_VALUE)
        );

        getContentPane().add(mainAreaPanel, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        refreshCurrentData();
        if (changeListener != null) changeListener.dataModified(DataModifiedListener.ABORT, getCurrentData());
        destroyWindow();
        formWindowClosing(null);
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        refreshCurrentData();
        if (changeListener != null) changeListener.dataModified(DataModifiedListener.SAVE, getCurrentData());
        destroyWindow();
        formWindowClosing(null);
    }//GEN-LAST:event_saveButtonActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        refreshCurrentData();
        if (changeListener != null) changeListener.dataModified(DataModifiedListener.WINDOW_CLOSED, getCurrentData());
    }//GEN-LAST:event_formWindowClosing

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private java.awt.Label formWindowTitle;
    private javax.swing.JPanel jPanel1;
    protected javax.swing.JPanel mainAreaPanel;
    private javax.swing.JButton saveButton;
    // End of variables declaration//GEN-END:variables
}
